package com.student.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.student.entity.Page;
import com.student.entity.Student;
import com.student.service.StudentService;

@Controller
public class StudentController {
	@Autowired
	private StudentService studentService;
	   
	public StudentService getStudentService() {
		return studentService;
	}

	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	/**
	 * 查询学生信息
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/selStudent")
	public ModelAndView selStudent(Page page) throws Exception {
		ModelAndView mv=new ModelAndView();
		mv.addObject("stulist", this.studentService.selStudent(page));
		mv.setViewName("index");
		return mv;
	}

	/**
	 * 添加学生信息
	 * @param stu
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addStudent")
	public ModelAndView addStudent(Student stu,Page page) throws Exception {
		this.studentService.addStudent(stu);
		ModelAndView mv=new ModelAndView();
		mv.addObject("stulist", this.studentService.selStudent(page));
		mv.setViewName("index");
		return mv;
	}
	
	/**
	 * 删除学生信息
	 * @param id
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delStudent")
	public ModelAndView delStudent(Integer id,Page page) throws Exception {
		this.studentService.delStudent(id); 
		ModelAndView mv=new ModelAndView();
		mv.addObject("stulist", this.studentService.selStudent(page));
		mv.setViewName("index");
		return mv;
	}
	
	/**
	 * 修改学生信息
	 * @param stu
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateStudent")
	public ModelAndView updateStudent(Student stu,Page page) throws Exception {
		this.studentService.updateStudent(stu);
		ModelAndView mv=new ModelAndView();
		mv.addObject("stulist", this.studentService.selStudent(page));
		mv.setViewName("index");
		return mv;
	}
	
	/**
	 * 点击修改 取出信息 跳转到修改页面
	 * @param id
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/update")
	public ModelAndView update(Integer id,Page page) throws Exception {
		ModelAndView mv=new ModelAndView();
		mv.addObject("stu", this.studentService.selStudent(id));
		mv.addObject("page", page);
		mv.setViewName("update");
		return mv;
	}
}
